JObj.use("ui");
//JObj.use('Tween');

JObj.UI.JAnimate = {};

(function($) {

    var objs = {};

    var Ani = function(obj) {
        var $ = this;
        var id = JObj.getUniqueId(obj);

        $.onComplete = null;
        $.onAbort = null;
        $.onStart = null;
        $.onProcess = null;

        var vars = {
            //d:500,//duration 持 续时间 ，越大越慢
            //t:0,//当前时间,
            frequence:30
        };

        $.setFrequence = function(p){
            if(isNaN(p)) return;
            vars.frequence = p;
        }

        var differ = function(from,to){
            /**
                * t：current time（当前时间）；
                * b：beginning value（初始值）；
                * c： change in value（变化量）；
                * d：duration（持续时间）。
              */

            /*if(vars.t < vars.d){
                return Math.ceil( Tween.Quart.easeOut(vars.t ,0,Math.max(from,to),vars.d) );
            }else{
                return to;
            }*/
            
            var v = Math.abs( to - from );
			v =  v / 5;
            if(v < 5) v = 5;
            return v;
        }

        $.run = function(fromRule,toRule){
        	clearTimeout(vars[id]);//must
        	var flag = false;
        	
        	
            var dW = fromRule.width < toRule.width ? 1 : -1;
            var dH = fromRule.height < toRule.height ? 1 : -1;
            var dA = fromRule.alpha < toRule.alpha ? 1 : -1;
            var dL = fromRule.left < toRule.left ? 1 : -1;
            var dT = fromRule.top < toRule.top ? 1 : -1;        	
        	
        	
            var run_ = function(){// inner function
                if(!flag){
                	flag = true;
                    JObj.isFunction($.onStart) ? $.onStart(obj) : null;
                }
                
                var cW = fromRule.width == toRule.width;
                var cH = fromRule.height == toRule.height;
                var cA = fromRule.alpha == toRule.alpha;
                var cL = fromRule.left == toRule.left;
                var cT = fromRule.top == toRule.top;                
                
                if(!cW){
                    fromRule.width += dW * differ(fromRule.width , toRule.width);
                    if(fromRule.width < 0) fromRule.width = 0;

                    if((dW == 1 && fromRule.width > toRule.width) || (dW == -1 && fromRule.width < toRule.width))
                        fromRule.width = toRule.width;

                    obj.style.width = fromRule.width + "px";
                }

                if(!cH){
                    fromRule.height += dH * differ(fromRule.height , toRule.height);
                    if(fromRule.height < 0 ) fromRule.height = 0;

                    if((dH == 1 && fromRule.height > toRule.height) || (dH == -1 && fromRule.height < toRule.height))
                        fromRule.height = toRule.height;

                    obj.style.height = fromRule.height + "px";
                }

                if(!cA){
                    fromRule.alpha += dA * differ(fromRule.alpha , toRule.alpha);
                    if(fromRule.alpha < 0) fromRule.alpha = 0;
                    if(fromRule.alpha > 100) fromRule.alpha = 100;

                    if((dA == 1 && fromRule.alpha > toRule.alpha) || (dA == -1 && fromRule.alpha < toRule.alpha))
                        fromRule.alpha = toRule.alpha;
                    
                    JObj.Style.css(obj,{alpha:fromRule.alpha});
                }

                if(!cT){
                    fromRule.top += dT * differ(fromRule.top , toRule.top);
                    if((dT == 1 && fromRule.top > toRule.top) || (dT == -1 && fromRule.top < toRule.top))
                        fromRule.top = toRule.top;

                    obj.style.top = fromRule.top + "px";
                }

                if(!cL){
                    fromRule.left += dL * differ(fromRule.left , toRule.left);
                    if((dL == 1 && fromRule.left > toRule.left) || (dL == -1 && fromRule.left < toRule.left))
                        fromRule.left = toRule.left;

                    obj.style.left = fromRule.left + "px";
                }

                if(cW && cH  && cA && cL && cT){
                    clearTimeout(vars[id]);
                    JObj.isFunction($.onComplete) ? $.onComplete(obj) : null;
                }else{
                    JObj.isFunction($.onProcess) ? $.onProcess(obj,fromRule,toRule) : null;
                }
            }
        
            vars[id] = setInterval(run_,vars.frequence);
        }
        
        $.runFromCurrent = function(toRule){
            setTimeout(function(){
                var fromRule = {};
                'width' in toRule && (fromRule.width = obj.clientWidth);
                'height' in toRule && (fromRule.height = obj.clientHeight);
                'left' in toRule && (fromRule.left = parseInt(JObj.Dom.getRuntimeStyle(obj,'left',0)));
                'top' in toRule && (fromRule.top = parseInt(JObj.Dom.getRuntimeStyle(obj,'top',0)));
                'alpha' in toRule && (fromRule.alpha = JObj.Dom.getOpacity(obj));
               $.run(fromRule,toRule);
            },0);
        }

        $.abort = function(){
            clearTimeout(vars[id]);
            JObj.isFunction($.onAbort) ? $.onAbort(obj) : null;
        }
        
        $.$ = function(pObj){
        	obj = pObj;
        	obj.setAttribute(JObj.getUniqueIdAttr(),id);
        }        
    }

    $.$ = function(obj){
        obj = JObj.$(obj);

        var uid = JObj.getUniqueId(obj);
        
        if(!(uid in objs)){
            objs[uid] = new Ani(obj);
        }
        return objs[uid];
    }

    $.unset = function(obj){
        delete objs[JObj.getUniqueId(obj)];
    }

    $.getObjs = function(){
        return objs;
    }
})(JObj.UI.JAnimate);